package org.dromara.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsOrder;
import org.dromara.wms.domain.WmsOrderLine;
import org.dromara.wms.service.WmsOrderServiceImpl;
import org.dromara.wms.service.WmsStockService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理
 * @author wenrui Su
 * @date 2024-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/order")
public class WmsOrderController extends BaseService {

  private final WmsOrderServiceImpl orderService;

  /**
   * 查询订单管理列表
   */
  @SaCheckPermission("wms:order:list")
  @GetMapping("/list")
  public TableDataInfo<WmsOrder> partnerList(WmsOrder order, PageQuery pageQuery) {
    return orderService.queryPageList(order, pageQuery);
  }


  /**
   * 导出订单管理列表
   */
  @SaCheckPermission("wms:order:export")
  @Log(title = "订单管理", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(WmsOrder bo, HttpServletResponse response) {
    List<WmsOrder> list = orderService.queryList(bo);
    ExcelUtil.exportExcel(list, "订单管理", WmsOrder.class, response);
  }

  /**
   * 获取订单管理详细信息
   * @param id 主键
   */
  @SaCheckPermission("wms:order:query")
  @GetMapping("/{id}")
  public R<WmsOrder> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
    return R.ok(orderService.queryById(id));
  }

  /**
   * 新增订单管理
   */
  @SaCheckPermission("wms:order:add")
  @Log(title = "订单管理", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody WmsOrder bo) throws WmsStockService.emptyException {
    return orderService.insertByBo(bo);
  }

  /**
   * 修改订单管理
   */
  @SaCheckPermission("wms:order:edit")
  @Log(title = "订单更新", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsOrder bo) {
    return toAjax(orderService.updateByBo(bo));
  }

  /**
   * 订单执行
   */
  @SaCheckPermission("wms:order:exec")
  @Log(title = "订单执行", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/execOrder")
  public R<Void> exec(@Validated(EditGroup.class) @RequestBody WmsOrder bo) throws WmsStockService.emptyException {
    return orderService.execByBo(bo);
  }

  /**
   * 转采购订单
   */
  @SaCheckPermission("wms:order:turn")
  @Log(title = "订单执行", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/turnOrder")
  public R<Void> turn(@Validated(EditGroup.class) @RequestBody List<WmsOrder> bo) throws WmsStockService.emptyException {
    return orderService.turnOrder(bo);
  }

  /**
   * 订单冲销
   */
  @SaCheckPermission("wms:order:back")
  @Log(title = "订单冲销", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/backOrder")
  public R<Void> back(@Validated(EditGroup.class) @RequestBody WmsOrder bo) {
    return orderService.backByBo(bo);
  }

  /**
   * 财务结算
   */
  @SaCheckPermission("wms:order:finance")
  @Log(title = "财务结算", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/financialSettlement")
  public R<Void> financialSettlement(@Validated(EditGroup.class) @RequestBody WmsOrder bo) {
    return orderService.financialSettlement(bo);
  }


  /**
   * 删除订单管理
   *
   * @param ids 主键串
   */
  @SaCheckPermission("wms:order:remove")
  @Log(title = "订单管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return toAjax(orderService.deleteWithValidByIds(List.of(ids), true));
  }

  /**
   * 查询测试从列表======================Line表的路由服务==========================
   */
  @SaCheckPermission("wms:order:list")
  @GetMapping("/lineList")
  public TableDataInfo<WmsOrderLine> lineList(WmsOrderLine bo, PageQuery pageQuery) {
    return orderService.queryLinePageList(bo, pageQuery);
  }

  /**
   * 删除测试从
   * @param lineIds 主键串
   */
  @SaCheckPermission("wms:order:remove")
  @Log(title = "测试从", businessType = BusinessType.DELETE)
  @DeleteMapping("/lineId/{lineIds}")
  public R<Void> removeLine(@NotEmpty(message = "主键不能为空") @PathVariable Long[] lineIds) {
    return toAjax(orderService.deleteWithValidByLineIds(List.of(lineIds), true));
  }

  /**
   * pc商城新增订单管理
   */
  @SaCheckPermission("wms:order:add")
  @Log(title = "订单管理", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping("/pcCart")
  public R<Void> pcAdd(@Validated(AddGroup.class) @RequestBody WmsOrder bo) throws WmsStockService.emptyException {
    return orderService.pcInsertByBo(bo);
  }
}
